<?php

namespace register\controller;

require_once("./login/model/LoginModel.php");
require_once("./register/view/RegisterView.php");
require_once("./register/model/RegisterModel.php");

class RegisterController {

	/**
	 * @var \register\view\LoginView
	 */
	private $view;

	/**
	 * @var \register\model\RegisterModel
	 */
	private $model;
	
	/**
	 * @param \register\view $view
	 */
	public function __construct(\register\view\RegisterView $view) {
		$this->view = $view;
		$this->model = new \register\model\RegisterModel();
	}
	
	/**
	 * Handle input
	 * Make sure to log statechanges
	 *
	 * note this has no output, output goes through views that are called seperately
	 */
	public function doToggleRegister() {
		if ($this->view->userPosting()) {
			try {
				$credentials = $this->view->getUserCredentials();
				$this->model->doRegister($credentials, $this->view);
				\Debug::log("Register succeded");
			} catch (\Exception $e) {
				\Debug::log("Register failed", false, $e->getMessage());
				$this->view->registerFailed();
			}											
		}
	}
}
